import stripe

from ..pay_strategy import PayStrategy
from ....config.pay_config import switched_env


class StripeStrategy(PayStrategy):

    def __init__(self, success_url, error_url, amount):
        stripe.api_key = switched_env['api']['api_key']
        self.stripe = stripe
        self.success_url = success_url
        self.error_url = error_url
        self.amount = amount

    def pay_out(self):
        amount_in_cents = int(round(self.amount, 2) * 100)
        ip_str = switched_env['env']['ip']
        port_str = switched_env['env']['port']
        checkout_session = self.stripe.checkout.Session.create(
            cancel_url=ip_str + ':' + port_str + self.error_url,
            success_url=ip_str + ':' + port_str + self.success_url,
            line_items=[
                {
                    'price_data': {
                        'currency': 'EUR',
                        'unit_amount': amount_in_cents,
                        'product_data': {
                            'name': 'Rectra订单支付',
                            'description': 'Rectra订单支付',
                        }
                    },
                    'quantity': 1,
                },
            ],
            mode='payment'
        )
        return checkout_session.get('url')
